package edu.cuit.avatar.thread;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/16 11:06
 */
public class TicketWindow extends Thread{
    private static int tickets = 1000;

    public TicketWindow(String name){
        super(name);
    }

    @Override
    public void run() {
        try {
            while(true){
                //
                synchronized (TicketWindow.class){
                    if(tickets <= 0)
                        break;
                    //Thread.currentThread():获取当前运行的线程
                    System.out.println(Thread.currentThread().getName() + "正在售出编号为 " + tickets-- + " 的火车票");
                    Thread.sleep(10);//模拟销售一张火车票的时间为10毫秒
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //创建10个线程，代表10个售票窗口
        for(var i = 0; i < 10; i++) {
            var thread = new TicketWindow("窗口-" + (i + 1));
            thread.start();//启动线程
        }
    }
}
